package com.lun.blog.controller.admin;

import com.lun.blog.pojo.Blog;
import com.lun.blog.pojo.Type;
import com.lun.blog.pojo.User;
import com.lun.blog.service.BlogService;
import com.lun.blog.service.TypeService;
import com.lun.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/blog")
    public String findBlog(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                           Model model,
                           BlogQuery b){
        model.addAttribute("types",typeService.findAll());
        model.addAttribute("page",blogService.listBlog(pageable,b));
        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         Model model,
                         BlogQuery b){
        model.addAttribute("page",blogService.listBlog(pageable,b));
        return "admin/blogs :: blogList";
    }

    @GetMapping("/blog/toSavePage")
    public String toSavePage(Model model){
        model.addAttribute("types",typeService.findAll());
        return "admin/blogs-input";
    }

    @PostMapping("/blog/addBlog")
    public String addBlog(Blog blog, HttpSession session,RedirectAttributes attributes){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        Blog blog1=null;
        if(blog.getId()!=null){
            blog1 = blogService.updateBlog(blog.getId(),blog);
        }else {
            blog1 = blogService.saveBlog(blog);
        }
        if(blog1==null){
            attributes.addFlashAttribute("message","博客添加失败");
        }else {
            attributes.addFlashAttribute("message","博客添加成功");
        }

        return "redirect:/admin/blog";
    }


    @GetMapping("/blog/toUpdateBlog")
    public String updateBlog(String id,Model model){
        //todo 更新博客信息
        Blog blog = blogService.getBlog(Long.parseLong(id));
        model.addAttribute("types",typeService.findAll());
        model.addAttribute("blog",blog);
        return "admin/blogs-update";
    }

    @GetMapping("/blog/deleteBlog")
    public String deleteBlog( String id, RedirectAttributes attributes){
        //todo 根据id删除博客信息
        boolean b = blogService.deleteBlog(Long.parseLong(id));
        if(b){
            attributes.addFlashAttribute("message","删除成功");
        }else {
            attributes.addFlashAttribute("message","删除失败");
        }
        return "redirect:/admin/blog";
    }
}
